﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.DAL.PrintManage;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.PrintManage;

namespace MINERP.BLL.PrintManage
{
    public  class PrinterConfigBLL
    {
        /// <summary>
        /// 获得Frontier类打印机发送设置历史信息列表
        /// </summary>
        /// <returns></returns>
        public List<PrinterConfigInfoModel> GetPrinterConfigInfoList()
        {
            List<PrinterConfigInfoModel> list = new List<PrinterConfigInfoModel>();
            using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
            {
                List<PrinterConfig> listPrinterConfig = entity.PrinterConfig.ToList();
                foreach (var item in listPrinterConfig)
                {
                    PrinterConfigInfoModel pc = new PrinterConfigInfoModel();
                    pc.PaperTypeName = item.PaperTypeName;
                    pc.PaperWidth = item.PaperWidth;
                    pc.PrinterConfigID = item.PrinterConfigID;
                    pc.SendNum = item.SendNum;
                    list.Add(pc);
                }
                
            }
            return list;
        }
        /// <summary>
        /// 向数据库中插入记录
        /// </summary>
        /// <param name="pc"></param>
        /// <returns></returns>
        public int Add(PrinterConfigInfoModel pc)
        {
            int ret = 0;
            try
            {
                using (newSampleYPDBEntities entity = new newSampleYPDBEntities())
                {
                    PrinterConfig pcInDB = entity.PrinterConfig.FirstOrDefault(p=>p.PaperTypeName ==pc.PaperTypeName&&p.PaperWidth ==pc.PaperWidth&&p.SendNum ==pc.SendNum);
                    if (pcInDB == null)
                    {
                        PrinterConfig printerConfig = new PrinterConfig();
                        printerConfig.PaperTypeName = pc.PaperTypeName;
                        printerConfig.PaperWidth = pc.PaperWidth;
                        printerConfig.SendNum = pc.SendNum;
                        entity.AddToPrinterConfig(printerConfig);
                        entity.SaveChanges();
                    }
                }
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }
    }
}
